package org.suyafei.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.suyafei.controller.request.TrafficPageRequest;
import org.suyafei.controller.request.UseTrafficRequest;
import org.suyafei.service.TrafficService;
import org.suyafei.utils.JsonData;
import org.suyafei.vo.TrafficVO;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 宿某人
 * @Date: 2022/03/14/17:28
 * @Description:
 */

@RestController
@RequestMapping("/api/traffic/v1")
public class TrafficController {


    @Autowired
    private TrafficService trafficService;

    @Value("${rpc.token}")
    private String token;


    @PostMapping("page")
    public JsonData pageAvailable(@RequestBody TrafficPageRequest request) {

        Map<String, Object> map = trafficService.pageAvailable(request);
        return JsonData.buildSuccess(map);
    }

    /**
     * 查询某个流量包详情
     *
     * @return
     */
    @GetMapping("/detail/{trafficId}")
    public JsonData detail(@PathVariable Long trafficId) {

        TrafficVO trafficVO = trafficService.detail(trafficId);
        return JsonData.buildSuccess(trafficId);


    }

    /**
     * 使用流量包API
     * @param request
     * @return
     */
    @PostMapping("reduce")
    public JsonData useTraffic(@RequestBody UseTrafficRequest useTrafficRequest , HttpServletRequest request){
        //使用流量包逻辑 TODO

        String requestToken = request.getHeader("rpc-token");
        if (token.equalsIgnoreCase(requestToken)) {
            return trafficService.reduce(useTrafficRequest);
        }else {
            return JsonData.buildError("非法访问");
        }
    }
}
